package com.sx.manage.controller.role;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.manage.biz.role.IMRoleBiz;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.relation.MWebRelationServiceImpl;
import com.sx.manage.service.role.IMBaseRoleInformationService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.manage.mybatis.form.role.MRoleForm;
import com.sx.model.manage.mybatis.vo.function.MFunctionInfoVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.role.MRoleVO;

@Controller
@RequestMapping("/role")
public class MRoleController extends MBaseController {
	/**
	 * 日志
	 */
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Resource(name = "mBaseRoleInfoServiceImpl")
	private IMBaseRoleInformationService mBaseRoleInfoServiceImpl;

	@Resource(name = "roleBizImpl")
	private IMRoleBiz roleBizImpl;

	@Resource(name = "webRelationServiceImpl")
	private MWebRelationServiceImpl webRelationServiceImpl;

	/**
	 * 跳转到 角色列表用户
	 */
	@RequestMapping(value = "roleIndex")
	public ViewAndModel roleIndex() {
		log.info("角色首页跳转");
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("role/role_index");

		return model;
	}

	/**
	 * 跳转到 角色列表用户
	 */

	@RequestMapping(value = "toRoleAdd")
	public ViewAndModel toRoleAdd() {
		log.info("角色添加页跳转");
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("role/role_add");

		// 获取角色编号
		String role_code = this.mBaseRoleInfoServiceImpl.codeUnique();
		model.addObject("role_code", role_code);

		return model;
	}

	/**
	 * 跳转到 角色列表用户
	 */
	@RequestMapping(value = "toRoleEdit/{id}")
	public ViewAndModel toRoleEdit(@PathVariable Long id) {
		log.info("角色编辑页跳转");
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("role/role_edit");

		ResultObjectVO<MRoleVO> objResult = this.roleBizImpl.queryById(id);

		model.addObject("role", objResult.getObjVo());
		return model;
	}

	/**
	 * 跳转到 角色列表用户
	 */
	@RequestMapping(value = "assignPower/{id}")
	public ViewAndModel assignPower(@PathVariable Long id) {
		log.info("角色配置模块界面id={}",id);
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("role/assignPower");

		ResultObjectVO<MRoleVO> objResult = this.mBaseRoleInfoServiceImpl.queryById(id);

		ResultListVO<MFunctionInfoVO> functionResult = webRelationServiceImpl.role2listForConfig(id, null);
		model.addObject("role", objResult.getObjVo());
		model.addObject("functionList", functionResult.getData());
		return model;
	}

	/**
	 * 获取翻页信息
	 * 
	 * @param roleVO
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value = "rolePage")
	@ResponseBody
	public ResultListVO<MRoleVO> rolePage(MRoleForm form) {
		log.info("角色列表数据获取form={}",form);
		return this.webRelationServiceImpl.rolePage(form, loginConsumer());

	}

	@RequestMapping(value = "roleList")
	@ResponseBody
	public ResultListVO<MRoleVO> roleList(MRoleForm form) {
		log.info("角色数据集合form={}",form);
		return this.mBaseRoleInfoServiceImpl.queryList(form, loginConsumer());

	}

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "roleAdd")
	@ResponseBody
	public ResultListVO roleAdd(MRoleForm form) {
		
		ResultListVO<?> listResult = null;
		if (form.getId() == null) {
			log.info("角色数据添加form={}",form);
			listResult = this.roleBizImpl.add(form, loginConsumer());

		} else if (form.getId() != null) {
			log.info("角色数据编辑form={}",form);
			listResult = this.roleBizImpl.edit(form, loginConsumer());
		}
		return listResult;
	}

	/**
	 * 根据id 获取 角色信息
	 * 
	 * @param roleVO
	 * @return
	 */
	@RequestMapping(value = "getRoleById")
	@ResponseBody
	public ResultObjectVO<MRoleVO> getRoleById(MRoleForm form) {
		log.info("根据id获取角色信息form={}",form);
		return this.mBaseRoleInfoServiceImpl.queryById(form.getId());

	}

	@RequestMapping(value = "deleteRoleById")
	@ResponseBody
	public ResultListVO<MRoleVO> deleteRoleById(Long id) {
		log.info("根据id删除角色信息id={}",id);
		return this.roleBizImpl.delete(id, loginConsumer());
	}


	public IMRoleBiz getRoleBizImpl() {
		return roleBizImpl;
	}

	public void setRoleBizImpl(IMRoleBiz roleBizImpl) {
		this.roleBizImpl = roleBizImpl;
	}

}
